package com.global.college.ui.presenter;

import android.content.Context;
import android.content.Intent;

import com.global.college.R;
import com.global.college.UserData;
import com.global.college.api.ProgressSubscriber;
import com.global.college.base.BasePresenter;
import com.global.college.data.inner.MoreLessonBean;
import com.global.college.data.inner.StudentBean;
import com.global.college.data.net.HomeGoodListBean;
import com.global.college.data.net.TrainDetailBean;
import com.global.college.ui.activity.TrainDetailBoughtActivity;
import com.global.college.ui.activity.TrainDetailWebActivity;
import com.global.college.ui.interfaces.IHomeSelectView;

import java.util.ArrayList;
import java.util.List;


/**
 * 下载文件
 */
public class HomeSelectPresenter extends BasePresenter {

    private IHomeSelectView mISelectView;

    public HomeSelectPresenter(Context context) {
        super(context);
    }

    public void setISelectView(IHomeSelectView iDownloadView) {
        mISelectView = iDownloadView;
    }

    public void getTrain() {
        mRequestClient.getGoodListBean(2).subscribe(new ProgressSubscriber<HomeGoodListBean>(mContext, false) {
            @Override
            public void onNext(HomeGoodListBean model) {
                if (null != mISelectView) {
                    mISelectView.onGetTrain(model);
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    public void getFreeCourse() {
        mRequestClient.getGoodListBean(1).subscribe(new ProgressSubscriber<HomeGoodListBean>(mContext, false) {
            @Override
            public void onNext(HomeGoodListBean model) {
                if (null != mISelectView) {
                    mISelectView.onGetCourse(model);
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    /**
     * 获取训练营详情
     *
     * @param trainId
     */
    public void getTrainDetail(String trainId, String goodId,String trainPriceStr) {
        mRequestClient.getTrainDetail(trainId, UserData.getInstance().getUserId()).subscribe(new ProgressSubscriber<TrainDetailBean>(mContext, true) {
            @Override
            public void onNext(TrainDetailBean model) {
                if (null != mISelectView) {
                    //区分已购买/未购买
                    if (model.getIsBuy().equals("YES")) {
                        mContext.startActivity(new Intent(mContext, TrainDetailBoughtActivity.class).putExtra("trainId", trainId));
                    } else {
                        mContext.startActivity(new Intent(mContext, TrainDetailWebActivity.class).putExtra("goodId", goodId).putExtra(TrainDetailWebActivity.VIDEO_ID, model.getVideo().getVideoId()).putExtra("trainId", trainId)
                                .putExtra("trainPriceStr",trainPriceStr));
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    public void getIsbought(String trainId) {
        mRequestClient.getTrainDetail(trainId, UserData.getInstance().getUserId()).subscribe(new ProgressSubscriber<TrainDetailBean>(mContext, true) {
            @Override
            public void onNext(TrainDetailBean model) {
                if (null != mISelectView) {
                    //区分已购买/未购买
//                    if (!model.getIsBuy().equals("YES")) {
//                        mISelectView.onGetBought(false);
//                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    /**
     * 免费商品直接调用创建订单接口，无需后续逻辑
     */
    public void createOrder(String userId, String goodId) {
        mRequestClient.createOrder(userId, goodId).subscribe(new ProgressSubscriber<Object>(mContext, true) {
            @Override
            public void onNext(Object object) {
            }
        });
    }

}
